package com.cartoonrabbit.controller;


import com.cartoonrabbit.service.IEarlyActionService;
import com.cartoonrabbit.vo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiuTeng
 * @since 2023-01-17
 */
@RestController
@RequestMapping("/early/action")
@Api(tags = "早起活动相关接口")
public class EarlyActionController {

    @Autowired
    private IEarlyActionService earlyActionService;

    @ApiOperation(value = "获取当前打卡活动和今日已打卡活动")
    @GetMapping("/twoActions")
    @PreAuthorize("@ss.permitAll()")
    public RespBean getTwoActions(Principal principal){
        if (ObjectUtils.isEmpty(principal)||ObjectUtils.isEmpty(principal.getName())){
            return RespBean.fail(401,"用户为授权，请重新登录");
        }
        return earlyActionService.getTwoActions(principal.getName());
    }

    @ApiOperation("早起打卡付费报名")
    @PreAuthorize("@ss.permitAll()")
    @PostMapping("/prePay")
    public RespBean prePay(Long money, Principal principal) throws Exception {

        if (ObjectUtils.isEmpty(principal)||ObjectUtils.isEmpty(principal.getName())){
            return RespBean.fail(401,"用户为授权，请重新登录");
        }

        if (ObjectUtils.isEmpty(money)||money<=0){
            return RespBean.fail("金额不能为空");
        }
        if (money!=100){
            return RespBean.fail("请支付金额一元");
        }

        return earlyActionService.doPrePay(money,principal.getName());
    }

    @ApiOperation("支付通知接口")
    @PreAuthorize("@ss.permitAll()")
    @PostMapping("/notify/{orderId}/{status}")
    public RespBean notify(@PathVariable String orderId, @PathVariable Integer status, Principal principal) throws Exception {

        if (ObjectUtils.isEmpty(principal)||ObjectUtils.isEmpty(principal.getName())){
            return RespBean.fail(401,"用户为授权，请重新登录");
        }

        if (ObjectUtils.isEmpty(orderId)||ObjectUtils.isEmpty(status)){
            return RespBean.fail(500,"参数异常");
        }

        return earlyActionService.doNotify(orderId,status,principal.getName());
    }

    @ApiOperation("用户打卡")
    @PreAuthorize("@ss.permitAll()")
    @PostMapping("/clock")
    public RespBean doClock(Integer actionId,Principal principal){
        if (ObjectUtils.isEmpty(principal)||ObjectUtils.isEmpty(principal.getName())){
            return RespBean.fail(401,"用户为授权，请重新登录");
        }
        if (ObjectUtils.isEmpty(actionId)){
            return RespBean.fail("参数异常");
        }
        return earlyActionService.doClock(actionId,principal.getName());
    }

}
